﻿using SpyApp.Helpers;
using SpyApp.Models;
using SpyApp.Models.BaseCommon;
using SpyApp.Models.BaseCommon.Enum;
using SpyApp.Models.EntytiRepossitory;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;

namespace SpyApp.Controllers
{
    public class AdminController : Controller
    {
        string _messageError = "";
        public UserRepository userRepository { get; set; }

        public AdminController()
        {
            this.userRepository = new UserRepository();
        }

        //
        // GET: /Admin/
        [Authorize(Roles = "Admin")]
        public ActionResult Index()
        {
            return View();
        }

        [Authorize(Roles = "Admin")]
        //[Authorize(Roles="admin")]
        public ActionResult Account()
        {
            int page = ConvertUtils.ToInt32(Request.QueryString[StaticValue.CURRENT_PAGE], 1);
            string searchString = Request.QueryString[StaticValue.QUICK_SEARCH_STRING];

            //Nghia: lấy danh sách account
            List<User> listUser = new List<Models.User>();
            SpyApp.Models.BaseCommon.ERROR_CODE.ERROR_CODE_ITEMS_SELECT errCode = UserRepository.Search(searchString, ref _messageError, ref listUser);
            if (errCode != Models.BaseCommon.ERROR_CODE.ERROR_CODE_ITEMS_SELECT.SUCCESS)
            {
                listUser = new List<User>();
            }
            CustomPageList<User> pageList = new CustomPageList<User>(
                listUser, 
                page, 
                StaticValue.DEFAUT_PAGESIZE);
            return View(pageList);
        }

        [Authorize(Roles = "Admin")]
        public ActionResult EditAccount(string id)
        {
            if (string.IsNullOrEmpty(id))
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            //Nghia: Chuyển user sang kiểu edit
            Models.User user = this.userRepository.GetUserById(id);
            AccountModel.AdminEdit accountEdit = new AccountModel.AdminEdit(user);

            return View(accountEdit);
        }

        [HttpPost]
        [AllowAnonymous]
        [Authorize(Roles = "Admin")]
        public ActionResult EditAccount(AccountModel.AdminEdit model)
        {
            if (ModelState.IsValid)
            {
                string id = Request.QueryString["id"];
                Models.User user = model.ToUser();
                // Nghĩa: em cần hàm update, dữ liệu em cập nhật trong model.ToUser();
                var result = this.userRepository.Update(id, user); //lỗi ạ
                if (result)
                {
                    ViewBag.MessageSuccess = "Saved";
                    return RedirectToAction("Account", "Admin");
                }
            }
            ModelState.AddModelError("", _messageError);
            return View(model);
        }
    }
}
